//
//  ListenViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ListenViewController.h"
#import "ZTFavorViewController.h"
#import "ZTHistoryViewController.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width

#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ListenViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *bigScrollView;

@property (nonatomic, strong) UIView *sliderView;

@property (nonatomic, strong) UIButton *favorBtn;

@property (nonatomic, strong) UIButton *historyBtn;

@end

@implementation ListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"定制听";
    


#pragma mark - 创建外层scrollview
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT)];
    self.bigScrollView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [self.view addSubview:self.bigScrollView];
    self.bigScrollView.contentSize = CGSizeMake(WIDTH * 2, HEIGHT);
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.bounces = NO;
    self.bigScrollView.delegate = self;
    
    
#pragma mark - 设置首页切换下的横条
    self.sliderView = [UIView new];
    self.sliderView.frame = CGRectMake(0, 40, WIDTH / 2, 2);
    self.sliderView.backgroundColor = [UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1];
    [self.view addSubview:self.sliderView];
    
#pragma mark - 创建两个viewController
    ZTHistoryViewController *historyVC = [[ZTHistoryViewController alloc] init];
    [self addChildViewController:historyVC];
    historyVC.view.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
    [self.bigScrollView addSubview:historyVC.view];
    
    
    ZTFavorViewController *favorVC = [[ZTFavorViewController alloc] init];
    favorVC.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self addChildViewController:favorVC];
    [self.bigScrollView addSubview:favorVC.view];
    

    
    
#pragma mark - 创建按钮
    // 推荐按钮
    self.favorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.favorBtn.frame = CGRectMake(0, 0, WIDTH / 2, 40);
    [self.favorBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
    [self.favorBtn setTitle:@"收藏" forState:UIControlStateNormal];
    self.favorBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.favorBtn];
    [self.favorBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 分类按钮
    self.historyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.historyBtn.frame = CGRectMake(self.favorBtn.frame.origin.x+self.favorBtn.frame.size.width, 0, WIDTH / 2, self.favorBtn.frame.size.height);
    [self.historyBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
    [self.historyBtn setTitle:@"历史" forState:UIControlStateNormal];
    self.historyBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.historyBtn];
    [self.historyBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    
    
    
}



-(void)buttonAction:(UIButton *)btn
{
    if (btn == self.favorBtn) {
        
        [self.bigScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
    }
    else
    {
        [self.bigScrollView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    }
    
}





-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.sliderView.frame = CGRectMake(self.bigScrollView.contentOffset.x / 2 , self.favorBtn.frame.origin.y + self.favorBtn.frame.size.height, self.favorBtn.frame.size.width, 2);
    
    if (self.bigScrollView.contentOffset.x == 0) {
        
        [self.favorBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
        
        [self.historyBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
    }
    
    if (self.bigScrollView.contentOffset.x == WIDTH) {
        
        [self.favorBtn setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1] forState:UIControlStateNormal];
        
        [self.historyBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
